package com.atguigu.ggkt.swagger.result;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class Result<T> {
    //状态码
    private Integer code;
    //返回信息
    private String message;
    //返回数据
    private T data;

    /**
     * 返回成功的json数据
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        Result<T> result = new Result<T>();
        if(data!=null){
            result.setData(data);
        }
        result.setCode(20000);
        result.setMessage("");
        return result;
    }

    /**
     * 返回失败的json数据
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(T data){
        Result<T> result = new Result<>();
        if(data!=null){
            result.setData(data);
        }
        result.setCode(20001);
        result.setMessage("失败");
        return result;
    }
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}

